home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 1 / QRZ Ham Radio Callsign Database - December 1993.iso / ucsd / packet / tcpip / sys5 / iscwmpst.z / iscwmpst / tcp / src / sys5unix.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-29  |  9.3 KB  |  476 lines

  1. #include <sys/types.h>
  2.  
  3. #include <signal.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <ctype.h>
  7. #include <sys/stat.h>
  8. #include <time.h>
  9. #include <termio.h>
  10. #include <unistd.h>
  11. #include <sys/bsdtypes.h>
  12. #ifdef SPASSWD
  13. #include <shadow.h>
  14. #endif
  15. #include <sys/fs/s5dir.h>
  16. #include <sys/user.h>
  17.  
  18. #include "sys5unix.h"
  19. #include "global.h"
  20. #include "iface.h"
  21. #include "timer.h"
  22. #include "files.h"
  23. #include "hardware.h"
  24. #include "main.h"
  25. #include "hpux.h" 
  26.  
  27. extern int  Debug;
  28. extern long  sigsetmask();
  29. extern void _exit();
  30. void abort();
  31.  
  32. #define TIMEOUT    120
  33.  
  34. static int  chkread[2];
  35. static int  actread[2];
  36. static void (*readfnc[_NFILE]) __ARGS((void *));
  37. static void *readarg[_NFILE];
  38.  
  39. static int  chkwrite[2];
  40. static int  actwrite[2];
  41. static void (*writefnc[_NFILE]) __ARGS((void *));
  42. static void *writearg[_NFILE];
  43.  
  44. static int  chkexcp[2];
  45. static int  actexcp[2];
  46. static void (*excpfnc[_NFILE]) __ARGS((void *));
  47. static void *excparg[_NFILE];
  48.  
  49. static int  nfds = -1;
  50.  
  51. static int  local_kbd;
  52.  
  53. static struct termio curr_termio;
  54. static struct termio prev_termio;
  55.  
  56. static void check_files_changed __ARGS((void));
  57.  
  58. /*---------------------------------------------------------------------------*/
  59. void ioinit()
  60. {
  61.  
  62.   int i;
  63.  
  64.   if (local_kbd = isatty(0)) {
  65.     ioctl(0, TCGETA, &prev_termio);
  66.     curr_termio = prev_termio;
  67.     curr_termio.c_iflag = BRKINT | ICRNL | IXON | IXANY | IXOFF;
  68.     curr_termio.c_lflag = 0;
  69.     curr_termio.c_cc[VMIN] = 0;
  70.     curr_termio.c_cc[VTIME] = 0;
  71.     ioctl(0, TCSETA, &curr_termio);
  72.     fflush(stdout);
  73.     on_read(0, keyboard, (void *) 0);
  74.   } else {
  75.     for (i = 0; i < _NFILE; i++) close(i);
  76.     setpgrp();
  77.     fopen("/dev/null", "r+");
  78.     fopen("/dev/null", "r+");
  79.     fopen("/dev/null", "r+");
  80.     remote_kbd_initialize();
  81.   }
  82.   setvbuf(stdout, NULL, _IOLBF, BUFSIZ); 
  83.   signal(SIGPIPE, SIG_IGN);
  84.   signal(SIGALRM, abort); 
  85.   signal (SIGCLD, sigchild_handler);  
  86.   if (!Debug) alarm(TIMEOUT);
  87.   umask(022);
  88.   if (!getenv("HOME"))
  89.     putenv("HOME=/users/root");
  90.   if (!getenv("LOGNAME"))
  91.     putenv("LOGNAME=root");
  92.   if (!getenv("PATH"))
  93.     putenv("PATH=/bin:/usr/bin:/usr/contrib/bin:/usr/local/bin");
  94.   if (!getenv("SHELL"))
  95.     putenv("SHELL=/bin/sh");
  96.   if (!getenv("TZ"))
  97.     putenv("TZ=MEZ-1MESZ");
  98.   timerproc();          /* Init times */
  99.   fixutmpfile();
  100. }
  101.  
  102. /*---------------------------------------------------------------------------*/
  103.  
  104. void iostop()
  105. {
  106.   register struct iface *ifp;
  107.  
  108.   if (local_kbd) {
  109.     ioctl(0, TCSETA, &prev_termio);
  110.     fflush(stdout);
  111.   }
  112.   for (ifp = Ifaces; ifp; ifp = ifp->next)
  113.     if (ifp->stop) (*ifp->stop)(ifp);
  114. }
  115.  
  116. /*---------------------------------------------------------------------------*/
  117. int system(cmdline)
  118. char *cmdline;
  119. {
  120.  
  121.   int  i, pid, status;
  122.   long  oldmask;
  123.  
  124.   if (!cmdline) return 1;
  125. sighold(SIGINT);
  126.  switch (pid = fork()) {
  127.   case -1:
  128. sigrelse(SIGINT);
  129.     return (-1);
  130.   case 0:
  131.     for (i = 3; i < _NFILE; i++) close(i);
  132.     sighold(SIGINT);
  133.     execl("/bin/sh", "sh", "-c", cmdline, (char *) 0);
  134.     sigrelse(SIGINT);
  135.     _exit(127); 
  136.   default:
  137.     sighold(SIGINT);
  138.     while (wait3(&status, -1, NULL) != pid)
  139.     sleep(-15); /* Wait some ticks */
  140.     sigrelse(SIGINT);
  141.     return status; 
  142.   }
  143. }
  144.  
  145. /*---------------------------------------------------------------------------*/
  146.  
  147. int  _system(cmdline)
  148. char  *cmdline;
  149. {
  150.   return system(cmdline);
  151. }
  152.  
  153. /*---------------------------------------------------------------------------*/
  154.  
  155. int  doshell(argc, argv, p)
  156. int  argc;
  157. char  *argv[];
  158. void *p;
  159. {
  160.   char  buf[2048];
  161.  
  162.   *buf = '\0';
  163.   while (--argc > 0) {
  164.     if (*buf) strcat(buf, " ");
  165.     strcat(buf, *++argv);
  166.   }
  167.   return system(buf);
  168. }
  169.  
  170. /*---------------------------------------------------------------------------*/
  171.  
  172. #define setmask(mask, fd) ((mask)[(fd)>>5] |=  (1 << ((fd) & 31)))
  173. #define clrmask(mask, fd) ((mask)[(fd)>>5] &= ~(1 << ((fd) & 31)))
  174. #define maskset(mask, fd) ((mask)[(fd)>>5] &   (1 << ((fd) & 31)))
  175.  
  176. /*---------------------------------------------------------------------------*/
  177.  
  178. void on_read(fd, fnc, arg)
  179. int  fd;
  180. void (*fnc) __ARGS((void *));
  181. void *arg;
  182. {
  183.   readfnc[fd] = fnc;
  184.   readarg[fd] = arg;
  185.   setmask(chkread, fd);
  186.   clrmask(actread, fd);
  187.   nfds = -1;
  188. }
  189.  
  190. /*---------------------------------------------------------------------------*/
  191.  
  192. void off_read(fd)
  193. int  fd;
  194. {
  195.   readfnc[fd] = 0;
  196.   readarg[fd] = 0;
  197.   clrmask(chkread, fd);
  198.   clrmask(actread, fd);
  199.   nfds = -1;
  200. }
  201.  
  202. /*---------------------------------------------------------------------------*/
  203.  
  204. void on_write(fd, fnc, arg)
  205. int  fd;
  206. void (*fnc) __ARGS((void *));
  207. void *arg;
  208. {
  209.   writefnc[fd] = fnc;
  210.   writearg[fd] = arg;
  211.   setmask(chkwrite, fd);
  212.   clrmask(actwrite, fd);
  213.   nfds = -1;
  214. }
  215.  
  216. /*---------------------------------------------------------------------------*/
  217.  
  218. void off_write(fd)
  219. int  fd;
  220. {
  221.   writefnc[fd] = 0;
  222.   writearg[fd] = 0;
  223.   clrmask(chkwrite, fd);
  224.   clrmask(actwrite, fd);
  225.   nfds = -1;
  226. }
  227.  
  228. /*---------------------------------------------------------------------------*/
  229.  
  230. void on_excp(fd, fnc, arg)
  231. int  fd;
  232. void (*fnc) __ARGS((void *));
  233. void *arg;
  234. {
  235.   excpfnc[fd] = fnc;
  236.   excparg[fd] = arg;
  237.   setmask(chkexcp, fd);
  238.   clrmask(actexcp, fd);
  239.   nfds = -1;
  240. }
  241.  
  242. /*---------------------------------------------------------------------------*/
  243.  
  244. void off_excp(fd)
  245. int  fd;
  246. {
  247.   excpfnc[fd] = 0;
  248.   excparg[fd] = 0;
  249.   clrmask(chkexcp, fd);
  250.   clrmask(actexcp, fd);
  251.   nfds = -1;
  252. }
  253.  
  254. /*---------------------------------------------------------------------------*/
  255.  
  256. static void check_files_changed()
  257. {
  258.  
  259.   int  changed = 0;
  260.   static long  nexttime, net_time, rc_time;
  261.   struct stat statbuf;
  262.  
  263.   if (Debug || nexttime > secclock()) return;
  264.   nexttime = secclock() + 600;
  265.  
  266.   if (stat("/tcp/net", &statbuf)) return;
  267.   if (!net_time) net_time = statbuf.st_mtime;
  268.   if (net_time != statbuf.st_mtime && statbuf.st_mtime < secclock() - 3600)
  269.     changed = 1;
  270.  
  271.   if (stat(Startup, &statbuf)) return;
  272.   if (!rc_time) rc_time = statbuf.st_mtime;
  273.   if (rc_time != statbuf.st_mtime && statbuf.st_mtime < secclock() - 3600)
  274.     changed = 1;
  275.  
  276.   if (changed) doexit(0, (char **) 0, (void *) 0);
  277. }
  278.  
  279. /*---------------------------------------------------------------------------*/
  280.  
  281. void eihalt()
  282. {
  283.  
  284.   int  n;
  285.   int  status;
  286.   int32 nte;
  287.   register int  nfds;
  288.   register unsigned int  i;
  289.   struct timeval timeout;
  290.  
  291.   check_files_changed();
  292.   wait3(&status, WNOHANG, (int *) 0); 
  293.   if (!Debug) alarm(TIMEOUT);
  294.   if (chkread[1] | chkwrite[1] | chkexcp[1]) {
  295.     i = chkread[1] | chkwrite[1] | chkexcp[1];
  296.     nfds = 32;
  297.   } else {
  298.     i = chkread[0] | chkwrite[0] | chkexcp[0];
  299.     nfds = 0;
  300.   }
  301.   for (n = 16; n; n >>= 1)
  302.     if (i & ((-1) << n)) {
  303.       nfds += n;
  304.       i >>= n;
  305.     }
  306.   if (i) nfds++;
  307.   actread [0] = chkread [0];
  308.   actread [1] = chkread [1];
  309.   actwrite[0] = chkwrite[0];
  310.   actwrite[1] = chkwrite[1];
  311.   actexcp [0] = chkexcp [0];
  312.   actexcp [1] = chkexcp [1];
  313.   timeout.tv_sec = 0;
  314.   if (Hopper)
  315.     timeout.tv_usec = 0;
  316.   else {
  317.     nte = next_timer_event();
  318.     if (nte > 999) nte = 999;
  319.     timeout.tv_usec = 1000 * nte;
  320.   }
  321.   if (select(nfds, actread, actwrite, actexcp, &timeout) < 1) {
  322.     actread [0] = actread [1] = 0;
  323.     actwrite[0] = actwrite[1] = 0;
  324.     actexcp [0] = actexcp [1] = 0;
  325.   } else
  326.     for (i = 0; i < nfds; i++) {
  327.       if (readfnc [i] && maskset(actread , i)) (*readfnc [i])(readarg [i]);
  328.       if (writefnc[i] && maskset(actwrite, i)) (*writefnc[i])(writearg[i]);
  329.       if (excpfnc [i] && maskset(actexcp , i)) (*excpfnc [i])(excparg [i]);
  330.     }
  331. }
  332.  
  333.  
  334. /* some other things */
  335.  
  336. rename(s1, s2)
  337. char *s1, *s2;
  338. {
  339.     char tmp[50];
  340.     int i;
  341.     unlink(s2);
  342.     i = link(s1, s2);
  343.     if(i == 0){
  344.             unlink(s1);
  345.     }
  346.     return(i);
  347. }
  348.  
  349. /* wait stuff */
  350.  
  351. #define MAXZOMBIE 32
  352. struct zombie {
  353.     int pid;
  354.     int status;
  355. } zombie [MAXZOMBIE];
  356.  
  357. int wait3 (statloc, options, dummy)
  358.     int *statloc;
  359.     int options;
  360.     int *dummy; {
  361.     int i, pid;
  362.     
  363.     do {
  364.         for (i = 0; i < MAXZOMBIE; i++)
  365.             if (zombie[i].pid) {
  366.                 if (statloc)
  367.                     *statloc = zombie[i].status;
  368.                 pid = zombie[i].pid;
  369.                 zombie[i].pid = 0;
  370.                 return pid;
  371.             }
  372.     } while (!options);
  373.     return 0;
  374. }
  375.  
  376. int
  377. sigchild_handler(sig, code)
  378.     int sig;
  379.     int code; {
  380.     int victim, status, i;
  381.     victim = wait (&status);
  382.     signal (sig, sigchild_handler);
  383.     for (i = 0; i < MAXZOMBIE; i++)
  384.         if (!zombie[i].pid) {
  385.             zombie[i].status = status;
  386.             zombie[i].pid = victim;
  387.             return;
  388.         }
  389. }
  390. #ifdef SPASSWD
  391. /** /etc/shadow processing **/
  392.  
  393. struct spwd *getspwdentry(name)
  394. char  *name;
  395. {
  396.  
  397. #define DEFAULTUSER    "guest"
  398.  
  399.   FILE * fp;
  400.   char  *cp;
  401.   char  username[128];
  402.   int  fd;
  403.   int  uid;
  404.   struct spwd *sw;
  405.  
  406.   /* Fix user name */
  407.  
  408.   for (cp = username; isalnum(uchar(*name)); *cp++ = tolower(uchar(*name++))) ;
  409.   *cp = '\0';
  410.   if (!isalpha(uchar(*username)) || strlen(username) > 8)
  411.     strcpy(username, DEFAULTUSER);
  412.  
  413.   /* Search existing shadow entry */
  414.  
  415.   while ((sw = getspent()) && strcmp(username, sw->sp_namp)) ;
  416.   endspent();
  417.   if (sw) return sw;
  418.   return 0;
  419. }
  420. #endif 
  421.  
  422. #ifndef RESTRICTED  /* ISC with bug allowing to set the u_uid etc. to 0 */
  423. int 
  424. setresuid(r,e,s)
  425. int r,e,s;
  426. {
  427.     struct user *user;
  428.     
  429.     user = (struct user *) 0xE0000000;
  430.  
  431.     user->u_uid = r;
  432.     user->u_ruid = e;
  433.     return 0;
  434. }
  435. int
  436. setresgid(r,e,s)
  437. int r,e,s;
  438. {
  439.     
  440.     struct user *user;
  441.     
  442.     user = (struct user *) 0xE0000000;
  443.  
  444.     user->u_gid = r;
  445.     user->u_rgid = e;
  446.     return 0;
  447. }
  448. #else
  449.  
  450. int setresuid(r,e,s)
  451. int r,e,s;
  452. {
  453. }
  454.  
  455. int setresgid(r,e,s)
  456. int r,e,s;
  457. {
  458. }
  459.  
  460. #endif /* RESTRICTED */
  461. /* Some dummy functions */
  462.  
  463. unsigned long strtoul (str, ptr, base)
  464. char *str, **ptr;
  465. int base;
  466. {
  467. return strtol(str, ptr, base);
  468. }
  469. rtprio()
  470. {
  471. }
  472.  
  473. settimeofday()
  474. {
  475. }
  476.